package com.shigen.demo.thread;

public class Race implements Runnable {

  @Override
  public void run() {
    for (int i = 0; i <= 100; i++) {
      if (Thread.currentThread().getName().equals("rabbit") && i % 10 == 0) {
        try {
          Thread.sleep(10);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      System.out.println(Thread.currentThread().getName() + "   " + i);
      if (gameOver(i)) {
        break;
      }
    }
    System.out.println("the winner: " + Thread.currentThread().getName());
  }

  private boolean gameOver(int steps) {
    return steps == 100;
  }

  public static void main(String[] args) {
    Race race = new Race();
    new Thread(race, "rabbit").start();
    new Thread(race, "tortoise").start();
  }
}
